//
//  LeftMenuViewController.m
//  CenturyInterconnection
//
//  Created by 郭先生 on 16/4/5.
//  Copyright © 2016年 Shanghai DuRui Information Technology Company. All rights reserved.
//

#import "LeftMenuViewController.h"
#import "LeftMenuModel.h"
#import "ProductListViewController.h"
#import "POPDViewController.h"


@interface LeftMenuViewController ()<UITableViewDelegate,UITableViewDataSource,SecondLevelMenuViewControllerDelegate>

@property(nonatomic,strong)SecondLevelMenuViewController * secondLevelMenuViewControllerr;

@property(nonatomic,strong)UIView * coverView;

@property(nonatomic,strong)NSString * cPath1;

@property(nonatomic,strong)NSMutableArray * arr2;


@end

@implementation LeftMenuViewController


-(UITableView *)tableView{
    if (!_tableView) {
        _tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight) style:UITableViewStyleGrouped];
        _tableView.delegate  = self;
        _tableView.dataSource= self;
        _tableView.separatorStyle= NO;
        _tableView.backgroundColor=[UIColor whiteColor];
    }
    
    return _tableView;
}

-(SecondLevelMenuViewController *)secondLevelMenuViewControllerr{
    if (!_secondLevelMenuViewControllerr) {
        _secondLevelMenuViewControllerr=[[SecondLevelMenuViewController alloc] init];
        _secondLevelMenuViewControllerr.delegate=self;
    }
    return _secondLevelMenuViewControllerr;
}


- (id)initWithCoder:(NSCoder *)aDecoder
{
    self.slideOutAnimationEnabled = YES;
    
    return [super initWithCoder:aDecoder];
}


#pragma mark - viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.arr2=[NSMutableArray array];
    
    [self getDatas:@"all"];
    
    
    [self.view addSubview:self.tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"SecondSectionTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell2"];
}


-(void)getDatas:(NSString *)cpathString {

    // http://viprunningonline.com/api/index.php?main_page=category
    
    NSString*urlStrr=[NSString stringWithFormat:@"%@",HTTP_ADDRESS];
    NSDictionary*paramss=@{@"main_page":@"category",
                           @"cPath":cpathString};
    HttpManager*managerr=[[HttpManager alloc]init];
    [managerr getDataFromNetworkWithUrl:urlStrr parameters:paramss compliation:^(id data, NSError *error) {
        
        NSString*number=[NSString stringWithFormat:@"%@",data[@"status"]];
        
        if ([number isEqualToString:@"1"]) {
//            for (int i=0; i<[data[@"data"] count]; i++) {
//                LeftMenuModel *model=[[LeftMenuModel alloc]initWithShopDict:data[@"data"][i]];
//                [self.arr2 addObject:model];
//    
//            }
            
            [self.arr2 addObjectsFromArray:data[@"data"]];
            
            [self.tableView reloadData];
        }
        
    }];
}

#pragma mark - tableview
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return 2;
    }else{
        return self.arr2.count;

    }
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];

    
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    cell.accessoryType=UITableViewCellAccessoryNone;
    
    UIImageView * rightImageView= [[UIImageView alloc] init];
    rightImageView.frame = CGRectMake(ACTUAL_WIDTH(280), ACTUAL_HEIGHT(11), ACTUAL_WIDTH(21),ACTUAL_HEIGHT(21));
    [cell.contentView addSubview:rightImageView];
    
    cell.textLabel.font = FONT_SYSTEM_28;
    cell.textLabel.textColor = RGBCOLOR(51,51,51,1) ;
    
    
    if (indexPath.section==0)
    {
        if(indexPath.row==0) {
            
            [rightImageView setImage:[UIImage imageNamed:@"menu_home"]];
            cell.textLabel.text=@"HOME";
            return cell;
        }
        
        if (indexPath.row==1){
            
            [rightImageView setImage:[UIImage imageNamed:@"menu_search"]];
            cell.textLabel.text=@"SEARCH";
            return cell;
        }
    }
    
    if (indexPath.section==1) {
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        rightImageView.frame=CGRectMake(ACTUAL_WIDTH(285), ACTUAL_HEIGHT(15), ACTUAL_WIDTH(9), ACTUAL_HEIGHT(14));
        [rightImageView setImage:[UIImage imageNamed:@"menu_next"]];
        
        LeftMenuModel *model=[[LeftMenuModel alloc] initWithShopDict: self.arr2[indexPath.row]];
        cell.textLabel.text=model.categories_name.uppercaseString;
        return cell;
        


    }
    
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section==0&&indexPath.row==0) {
        
        HomePageViewController * homeVC = [[HomePageViewController alloc] init];
        [[SlideNavigationController sharedInstance]  popToRootAndSwitchToViewController:homeVC withCompletion:^{}];
    }
    
    if (indexPath.section==0&&indexPath.row==1){
        
        HomePageViewController * homeVC = [[HomePageViewController alloc] init];
        homeVC.isOpenSearch = YES;
        
        
        if([self.delegate respondsToSelector:@selector(delegateForSearchBecomeFirst)]){
            [self.delegate delegateForSearchBecomeFirst];
        }
        
        [[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:homeVC withCompletion:^{}];
        
     
    }
    
    
    if (indexPath.section==1) {
        
        //LeftMenuModel * model = [[LeftMenuModel alloc] initWithShopDict:self.arr2[indexPath.row]];
        
        SecondLevelMenuViewController * seLVC = [[SecondLevelMenuViewController alloc] init];
        seLVC.titleStr = self.arr2[indexPath.row][@"categories_name"];
        seLVC.titleArray = self.arr2[indexPath.row][@"child"];
        [self presentViewController:seLVC animated:YES completion:^{} ];

    }
    
}



#pragma mark - 设置头界面的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return ACTUAL_HEIGHT(20);
    }else{
        return 0.00001;
    }
}

//尾界面高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section==0) {
        return ACTUAL_HEIGHT(15);
    }else{
        return 0.00001;
    }
}

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *BGView=[[UIView alloc]initWithFrame:CGRectMake(0, ACTUAL_HEIGHT(0), KScreenWidth , ACTUAL_HEIGHT(15))];
    BGView.backgroundColor=RGBCOLOR(233, 234, 235, 1);
    return BGView;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
